package com.sx.pipeline.ui.chat.organize

import android.view.View
import android.widget.RelativeLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.sx.pipeline.BR
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActivityOrganizeBinding
import com.sx.pipeline.ui.chat.ChatVM
import com.sx.pipeline.view.treeview2.Node
import com.sx.pipeline.view.treeview2.NodeHelper
import com.sx.pipeline.view.treeview2.NodeTreeAdapter
import com.sx.pipeline.view.treeview2.OrganizeDeptRes
import java.util.*


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 组织架构
 */
@Route(path = RoutePath.Chat.CHAT_ORGANIZE)
class OrganizeActivity : CYDataBindingBaseActivity<ActivityOrganizeBinding, ChatVM>(
    com.sx.pipeline.R.layout.activity_organize
), View.OnClickListener {

    private var mAdapter: NodeTreeAdapter? = null
    private val mLinkedList = LinkedList<Node<*>>()

    override fun initData() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this)
            .fitsSystemWindows(true)
            .statusBarColor(com.sx.pipeline.R.color.color_0188ff)
            .statusBarDarkFont(false)
            .init()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mViewModel.getRongOrganize()



        mAdapter = NodeTreeAdapter(this, mBinding.treeview, mLinkedList)
        mBinding.treeview.setAdapter(mAdapter)
    }

    private fun addOne(data: MutableList<Node<*>>,res:List<OrganizeDeptRes>) {
        for (i in res.indices) {
            data.add(res[i])
        }
    }


    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.organizeListEvent){
            val data: MutableList<Node<*>> = ArrayList()
            it?.let { it1 -> addOne(data, it1) }
            mLinkedList.addAll(NodeHelper.sortNodes(data))
            mAdapter!!.notifyDataSetChanged()
        }
    }

//    private fun initRv(list:List<Node>){
//        rl = RelativeLayout(this)
//        rl!!.layoutParams = RelativeLayout.LayoutParams(
//            RelativeLayout.LayoutParams.MATCH_PARENT,
//            RelativeLayout.LayoutParams.MATCH_PARENT)
//        listView = TreeListView(this,list)
//        listView.setLayoutParams(
//            RelativeLayout.LayoutParams(
//                RelativeLayout.LayoutParams.MATCH_PARENT,
//                RelativeLayout.LayoutParams.MATCH_PARENT
//            )
//        )
//        mBinding.mainRelativeLayout.addView(listView)
//    }

    override fun onClick(v: View?) {
    }
}